<template>
  <!-- 忘记密码 -->
  <div class="forgetPW">
    <el-dialog v-model="state.forgetPasswordDialog" title="忘记密码" width="30%">
      <el-form class="login-form" :label-position="labelPosition" :rules="rules">
        <el-form-item label="输入您的注册账号" prop="account">
          <el-input v-model="forgetData.account" placeholder="输入您的注册账号" />
        </el-form-item>
        <el-form-item label="输入您的个人邮箱" prop="email">
          <el-input v-model="forgetData.email" placeholder="输入您的个人邮箱" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="state.forgetPasswordDialog = false">取消</el-button>
          <el-button type="primary" @click="nextUpadatePW">
            下一步
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
  <!-- 修改密码 -->
  <div class="forgetPW">
    <el-dialog v-model="state.changePasswordDialog" title="修改密码" width="30%" :modal="state.isModel">
      <el-form class="login-form" :label-position="labelPosition" :rules="rules">
        <el-form-item label="请输入新密码" prop="password">
          <el-input v-model="forgetData.password" placeholder="新密码"type="password" />
        </el-form-item>
        <el-form-item label="请再次输入新密码" prop="repassword" >
          <el-input v-model="forgetData.repassword" placeholder="确认密码" type="password"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="backForgetPW">上一步</el-button>
          <el-button type="primary" @click="updatePW">
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue'
import { verifyAPI, resetAPI } from '../../../api/login'
import { ElMessage } from 'element-plus'

const labelPosition = ref('top')



const state = reactive({
  forgetPasswordDialog: false,
  changePasswordDialog: false,

  isModel: true
})

const openDialog = () => {
  state.forgetPasswordDialog = true
}

interface forgetData {
  account: number | null
  email: string
  password: string
  repassword: string
}


const forgetData: forgetData = reactive({
  account: null,
  email: '',
  password: '',
  repassword: '',
})

// 表单校验
const rules = reactive({
  account: [
    { required: true, message: '请输入您的注册账号', trigger: 'blur' },
  ],
  email: [
    { required: true, message: '请输入您的注册邮箱', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入您想修改的密码', trigger: 'blur' },
  ],
  nextPassword: [
    { required: true, message: '请再次确认您的新密码', trigger: 'blur' },
  ],
})

// 上一步
const backForgetPW = () => {
  state.isModel = false
  setTimeout(() => {
    state.forgetPasswordDialog = true
  }, 200)
}

// 忘记密码 --- 验证账号和邮箱
const nextUpadatePW = async () => {
  const res = await verifyAPI({
    account: forgetData.account,
    email: forgetData.email
  })
  if (res.data.status === 0) {
    ElMessage({
      message: '验证成功',
      type: 'success',
    })
    localStorage.setItem('id', res.data.id)
    state.forgetPasswordDialog = false
    state.changePasswordDialog = true
    state.isModel = true
  } else if (res.data.message === '用户不存在') {
    ElMessage({
      message: '用户不存在',
      type: 'error',
    })
  }else {
    ElMessage({
      message: '账号和邮箱不匹配, 请检查',
      type: 'error',
    })
  }
}

// 重置密码 --- 修改密码
const updatePW = async () => {
  if(forgetData.password !== forgetData.repassword) {
    ElMessage({
      message: '两次输入的密码不一致, 请检查',
      type: 'error',
    })
  }else {
    const res = await resetAPI({
    id: localStorage.getItem('id'),
    newPassword: forgetData.password,
  })
  ElMessage({
      message: '验证成功',
      type: 'success',
    })
    state.changePasswordDialog = false
  }
}

defineExpose({
  openDialog
})
</script>

<style lang="scss" scoped>
</style>